package com.yima.baseproj.module.app.user;

import com.yima.baseproj.config.redis.lock.Callback;
import com.yima.baseproj.config.redis.lock.RedisLockUtil;
import com.yima.baseproj.utils.AppContextUtil;

import java.util.concurrent.TimeUnit;

public class AppUserNoUtil {

    /**
     * 创建用户编号
     * @return
     */
    public static String createUserNo(){
        return (String) RedisLockUtil.getInstance().execute("USER:NO", 3, TimeUnit.SECONDS, new Callback() {
            @Override
            public String onGetLock() throws InterruptedException {
                AppUserService appUserService = AppContextUtil.getBean(AppUserService.class);
                AppUserEntity appUserEntity = appUserService.findByMaxNo();
                if(appUserEntity == null){
                    return "10000000";
                }
                return String.valueOf(Integer.valueOf(appUserEntity.getUserNo()) + 1);
            }

            @Override
            public String onTimeout() throws InterruptedException {
                return null;
            }
        });
    }
}
